package com.hc.fdkforwarders.dao;

import ch.qos.logback.core.joran.conditional.ElseAction;
import com.hc.fdkforwarders.common.utils.StringUtils;
import com.hc.fdkforwarders.param.PrintLableParam;
import com.hc.fdkforwarders.param.TagPrintedParam;
import com.hc.fdkforwarders.vo.GoodsScannedVO;
import com.hc.fdkforwarders.vo.NumSumListQueryVO;
import com.hc.fdkforwarders.vo.NumSumListVO;
import com.hc.fdkforwarders.vo.PrintLabelVO;
import org.jooq.SelectConditionStep;
import org.jooq.SelectFromStep;
import org.jooq.SelectWhereStep;
import org.jooq.impl.DSL;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static com.hc.fdkforwarders.db.tables.SellerFetchOrder.SELLER_FETCH_ORDER;
import static org.jooq.impl.DSL.countDistinct;


import static com.hc.fdkforwarders.db.tables.PlatformSpu.PLATFORM_SPU;

@Repository
public class SellerFetchOrderDao extends BaseDao{

    // 更改小标签状态 状态：@1拿货中 @2已打印 @3已完结
    public void updateStatus(String barcode, Byte status){
        db.update(SELLER_FETCH_ORDER)
                .set(SELLER_FETCH_ORDER.STATUS, status)
                .where(SELLER_FETCH_ORDER.PLATFORM_BARCODE.eq(barcode))
                .execute();
    }

    // 更改tagflag 0：未打印 1：已打印
    public void updateTagPrintFlag(String barcode, Byte flag){
        db.update(SELLER_FETCH_ORDER)
                .set(SELLER_FETCH_ORDER.TAG_PRINT_FLAG, flag)
                .where(SELLER_FETCH_ORDER.PLATFORM_BARCODE.eq(barcode))
                .execute();
    }

    public GoodsScannedVO goodsScanned(String barcode){

        return db.select(SELLER_FETCH_ORDER.PLATFORM_BARCODE.as("barcode"), SELLER_FETCH_ORDER.BIZ_NAME.as("bizFullName"),
                SELLER_FETCH_ORDER.SELLER_QR_CODE.as("qrCode"), SELLER_FETCH_ORDER.SELLER_NAME, SELLER_FETCH_ORDER.SELLER_SHORT_NAME,
                SELLER_FETCH_ORDER.SKU_NAME, SELLER_FETCH_ORDER.SPU_GOODS_NO, PLATFORM_SPU.MAIN_IMG_URL.as("imgUrl"),
                SELLER_FETCH_ORDER.PLATFORM_SKU_ID.as("skuId"), SELLER_FETCH_ORDER.STATUS)
                .from(SELLER_FETCH_ORDER)
                .innerJoin(PLATFORM_SPU)
                .on(SELLER_FETCH_ORDER.PLATFORM_SPU_ID.eq(PLATFORM_SPU.ID))
                .where(SELLER_FETCH_ORDER.PLATFORM_BARCODE.eq(barcode))
                .fetchOneInto(GoodsScannedVO.class);
    }

    public void tagPrinted(TagPrintedParam tagPrintedParam){
        for(Long id:tagPrintedParam.getIdList()){
            db.update(SELLER_FETCH_ORDER)
                    .set(SELLER_FETCH_ORDER.STATUS, new Byte("2"))
                    .set(SELLER_FETCH_ORDER.TAG_PRINT_FLAG, new Byte("1"))
                    .where(SELLER_FETCH_ORDER.ID.eq(id)).execute();
        }
    }
    //代拿接口2、、NumSumListQueryVO queryVO
    public List<NumSumListVO> showGoodsList(NumSumListQueryVO queryVO){
        SelectFromStep<?> sel = db.select(SELLER_FETCH_ORDER.MERCHANT_ID,
                SELLER_FETCH_ORDER.SELLER_NAME,
                SELLER_FETCH_ORDER.SELLER_SHORT_NAME,
                DSL.countDistinct(SELLER_FETCH_ORDER.PLATFORM_SKU_ID).as("totalKind"), //SKUID区分种类
                DSL.count(SELLER_FETCH_ORDER.ID).as("totalNum"));
        SelectWhereStep<?> sf = sel.from(SELLER_FETCH_ORDER);
        SelectConditionStep<?> query = sf.where(SELLER_FETCH_ORDER.BIZ_NAME.eq(queryVO.getBizFullName()));
        if(queryVO.getCityId()!=null){
            query = query.and(SELLER_FETCH_ORDER.CITY_ID.eq(queryVO.getCityId()));
        }

        if(queryVO.getMarketId()!=null){
            query = query.and(SELLER_FETCH_ORDER.MARKET_ID.eq(queryVO.getMarketId()));
        }

        if(queryVO.getFloorId()!=null){
            query = query.and(SELLER_FETCH_ORDER.FLOOR_CODE.eq(queryVO.getFloorId()));
        }

        if(!StringUtils.isEmpty(queryVO.getSpuGoodsNo())){
            query = query.and(SELLER_FETCH_ORDER.SPU_GOODS_NO.like("%"+queryVO.getSpuGoodsNo()+"%"));
        }

        if(queryVO.getStatus()!=null){
            query = query.and(SELLER_FETCH_ORDER.STATUS.eq(queryVO.getStatus()));
        }
        return query.groupBy(SELLER_FETCH_ORDER.MERCHANT_ID).fetchInto(NumSumListVO.class);
    }

    //打印小标签
    public List<PrintLabelVO> printLabel(PrintLableParam param){
        List<PrintLabelVO> allLabel = new ArrayList<>();
        SelectConditionStep<?> query = db.select(SELLER_FETCH_ORDER.PLATFORM_BARCODE.as("barcode")
                ,SELLER_FETCH_ORDER.BIZ_NAME.as("bizFullName")
                ,SELLER_FETCH_ORDER.ID
                ,SELLER_FETCH_ORDER.SELLER_QR_CODE.as("qrCode")
                ,SELLER_FETCH_ORDER.SELLER_SHORT_NAME
                ,SELLER_FETCH_ORDER.SKU_NAME
                ,SELLER_FETCH_ORDER.STATUS
                ,SELLER_FETCH_ORDER.SPU_GOODS_NO)
                .from(SELLER_FETCH_ORDER)
                .where(SELLER_FETCH_ORDER.CITY_ID.eq(param.getCityId()));
        if(!StringUtils.isEmpty(param.getBizFullName())){
            query = query.and(SELLER_FETCH_ORDER.BIZ_NAME.eq(param.getBizFullName()));
        }
        if(param.getFloorId()!=null){
            query = query.and(SELLER_FETCH_ORDER.FLOOR_CODE.eq(param.getFloorId()));
        }
        if(param.getMarketId()!=null){
            query = query.and(SELLER_FETCH_ORDER.MARKET_ID.eq(param.getMarketId()));
        }
        if(!StringUtils.isEmpty(param.getSpuGoodsNo())){
            query = query.and(SELLER_FETCH_ORDER.SPU_GOODS_NO.like("%"+param.getSpuGoodsNo()+"%"));
        }
        byte tmp = 0;
        if(param.getNumType() == 2){
            query = query.and(SELLER_FETCH_ORDER.TAG_PRINT_FLAG.eq(tmp));
        }
        if(!param.getSelectAll()){
            for(Long skuId:param.getSkuIdList()){
                query = query.and(SELLER_FETCH_ORDER.PLATFORM_SKU_ID.eq(skuId));
                List<PrintLabelVO> w = query.fetchInto(PrintLabelVO.class);
                allLabel.addAll(w);
            }
        }else{
            allLabel = query.fetchInto(PrintLabelVO.class);
        }
        return allLabel;
    }
}
